package com.ciis.base.system;

import com.ciis.base.Constants;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: zhangdajun
 * Date: 14-4-12
 * Time: 上午12:18
 * To change this template use File | Settings | File Templates.
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        String[] noFilters = new String[] {"loginBack.do","checkUser.do","intoLogin.do"};
        String uri = request.getRequestURI();

        if (uri.indexOf("back") != -1) {
            boolean beFilter = true;
            for (String s : noFilters) {
                if (uri.indexOf(s) != -1) {
                    beFilter = false;
                    break;
                }
            }
            if (beFilter) {
                Object obj = request.getSession().getAttribute(
                        Constants.USER_INFO);
                if (null == obj) {
                    // 没有登录，重定向到登录页面
                    response.sendRedirect("/back/sysuser/intoLogin.do");
                    return false;
                }
            }
        }
        return super.preHandle(request, response, handler);
    }

}
